<?php
namespace app\admin\controller;
use think\Db;
use think\facade\Request;

class StarCard extends Common{

	###球星卡列表###
	public function index(){
        if(request()->isPost()){
            $key=input('post.key');
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');
            $list=db('star_card')
                ->field('id,nickname,country,status')
                ->where('nickname','like',"%".$key."%")
                ->where('status','eq',1)
                ->order('id asc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();
			$card = Db::name('star_card_collect')->field('card_id,sum(num) num')->where('status','eq',1)->group('card_id')->select();
			$count= Db::name('star_card_collect')->field('sum(num) num')->where('status','eq',1)->find();
			foreach($card as $k => $v){
				foreach($list['data'] as $k1 => $v1){
					if($v1['id'] == $v['card_id']){
						$list['data'][$k1]['num'] = $v['num'];
						$win = ((intval($v['num'])/intval($count['num']))*100);
						$list['data'][$k1]['win_rate'] = substr($win,0,3)."%";
					}
				}
			}
            // var_dump($count);
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }
        return $this->fetch();		
	}
}